﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using GameSPK.DAO;

namespace GameSPK.Content
{
    /// <summary>
    /// Interaction logic for frmNhapNguoiChoi.xaml
    /// </summary>
    public partial class frmNhapNguoiChoi : Window
    {
        public frmNhapNguoiChoi()
        {
            InitializeComponent();
            nguoichoi = new NguoiChoi();
        }
        string filevideo;
        bool ischonfile = false;
        NguoiChoi nguoichoi;
        private void btnChonFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            ImageBrush myBrush = new ImageBrush();
            openDialog.Filter = "Image Files (*.bmp, *.jpg, *.gif, *.png, *.flv, *.avi, *.mp4, *.wmv)|*.bmp;*.jpg;*.gif; *.png; *.flv; *.avi; *.mp4; *.wmv";
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filevideo = openDialog.FileName;

                System.IO.File.Copy(filevideo, @"media\" + openDialog.SafeFileName, true);
                filevideo = @"media\" + openDialog.SafeFileName;
                ischonfile = true;
                this.mEVideo.Source = new Uri(filevideo, UriKind.RelativeOrAbsolute);
                mEVideo.Play();
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (!isNhapXong())
            {
                System.Windows.Forms.MessageBox.Show("Nhập tất cả các thông tin");
            }
            nguoichoi.MaNguoiChoi = Helper.GenerateKey();
            nguoichoi.TenNguoiChoi = txtTenNguoiChoi.Text;
            nguoichoi.ThongTin = txtThongTin.Text;
            nguoichoi.ChonThi = cbChonThi.IsChecked;
            nguoichoi.Video = filevideo;
            NguoiChoiDAO.NhapNguoiChoi(nguoichoi);
            NguoiChoiDAO.ChonNguoiChoiCuocThi(nguoichoi, MainWindow.getCuocThiChon());
            clear();
        }
        void clear()
        {
            this.txtTenNguoiChoi.Text = "";
            this.txtThongTin.Text = "";
            ischonfile = false;
            
        }
        bool isNhapXong()
        {
            if (txtTenNguoiChoi.Text == "")
                return false;
            if (txtThongTin.Text == "")
                return false;
            if (ischonfile == false)
                return false;
            return true;
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mEVideo.LoadedBehavior = MediaState.Manual;
        }
    }
}
